package com.weiyuwqj.admin.controller.admin.sys;

import com.weiyuwqj.api.service.SysAppPushService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.SysAppPush;
import com.weiyuwqj.api.service.NoticePush;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/3/18.
 */
@Controller
public class AppPushController {
    @Autowired
    SysAppPushService appPushService;

    @Autowired
    NoticePush noticePush;

    @ResponseBody
    @RequestMapping(value="/admin/push/add",method= RequestMethod.POST)
    public Object add(SysAppPush sysAppPush){
        sysAppPush.setAddtime(new Date());
        return appPushService.save(sysAppPush);
    }


    @ResponseBody
    @RequestMapping(value="/admin/push/edit",method = RequestMethod.POST)
    public void edit(SysAppPush sysAppPush){
        appPushService.updateNotNull(sysAppPush);
    }


    @ResponseBody
    @RequestMapping(value="/admin/push/getSingle", method=RequestMethod.POST)
    public Object getSingle(@RequestParam(defaultValue = "0") Integer id){
        return appPushService.selectByKey(id);
    }

    @ResponseBody
    @RequestMapping(value="/admin/push/getList", method=RequestMethod.POST)
    public Object getList(Paginator paginator,String title,String beginDateTime,String endDateTime){
        Map<String, Object> map = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(title)){
            map.put("title", title);
        }
        if(StringUtils.isNotEmpty(beginDateTime)){
            map.put("beginDateTime", beginDateTime);
        }
        if(StringUtils.isNotEmpty(endDateTime)){
            map.put("endDateTime", endDateTime);
        }
        return appPushService.getList(paginator, map);
    }

    /**
     * 推送
     * @param id
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value="/admin/push", method=RequestMethod.POST)
    public Object push(Integer id) throws IOException {
        SysAppPush push=appPushService.selectByKey(id);
        String title=push.getPushTitle();
        String text=push.getPushText();
        noticePush.allPush(title,text);
        return Message.OK;
    }



}
